package com.xfgg.cloud.app;

import com.xfgg.cloud.adapter.request.GameRequest;
import com.xfgg.cloud.adapter.request.LoginRequest;
import lombok.RequiredArgsConstructor;
import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.AnalyzeRequest;
import org.elasticsearch.client.indices.AnalyzeResponse;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * es相关操作
 *
 * @author xfgg
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ElasticSearchService {

    private final RestHighLevelClient restHighLevelClient;


    public void loginLog(LoginRequest loginRequest) {
        Map<String, Object> jsonMap = new HashMap<>(2);
        jsonMap.put("userName", loginRequest.getUserName());
        jsonMap.put("passWord", loginRequest.getPassWord());
        IndexRequest indexRequest = new IndexRequest("user_login").source(jsonMap).opType(DocWriteRequest.OpType.CREATE);
        try {
            IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
            restHighLevelClient.close();
        } catch (Exception e) {
            throw new RuntimeException("");
        }
    }

    public List<LoginRequest> getLoginLogByTerm(String search) {
        List<LoginRequest> loginRequests = new ArrayList<>();
        try {
            SearchRequest searchRequest = new SearchRequest("user_login");
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(QueryBuilders.termQuery("userName", search));
            searchRequest.source(searchSourceBuilder);
            SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            for (SearchHit hit : searchResponse.getHits().getHits()) {
                Map<String, Object> sourceMap = hit.getSourceAsMap();
                String userName = (String) sourceMap.get("userName");
                String passWord = (String) sourceMap.get("passWord");
                LoginRequest loginRequest = new LoginRequest();
                loginRequest.setUserName(userName);
                loginRequest.setPassWord(passWord);
                loginRequests.add(loginRequest);
            }
            restHighLevelClient.close();
        } catch (Exception e) {
            return new ArrayList<>();
        }
        return loginRequests;
    }

    public void putGameData(GameRequest gameRequest) {

    }

    public List<AnalyzeResponse.AnalyzeToken> analyzeSearchTerms(String analyzer, String query) {
        AnalyzeRequest analyzeRequest = AnalyzeRequest.withGlobalAnalyzer(analyzer, query);
        try {
            AnalyzeResponse analyze = restHighLevelClient.indices().analyze(analyzeRequest, RequestOptions.DEFAULT);
            return analyze.getTokens();
        } catch (Exception e) {
            return Collections.emptyList();
        }
    }
}
